package com.windgo.controller.wx;

import com.github.pagehelper.PageInfo;
import com.windgo.bean.MarketRole;
import com.windgo.bean.MarketUser;
import com.windgo.bean.bo.FootprintBO;
import com.windgo.bean.common.BaseRespVo;
import com.windgo.bean.common.CommonData;
import com.windgo.bean.vo.WxFootprintListVO;
import com.windgo.service.WxCartService;
import com.windgo.service.WxFootprintService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("wx/footprint")
public class WxFootprintController {
    @Autowired
    WxFootprintService wxFootprintService;

    @RequestMapping("list")
    public BaseRespVo<CommonData<WxFootprintListVO>> wxFootprintList(Integer page,Integer limit){
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
//        Integer userId = 1;
        CommonData<WxFootprintListVO> commonData = new CommonData<>();
        PageInfo<WxFootprintListVO> pageInfo = wxFootprintService.listWxFootprint(page,limit,userId);
        int total = Long.valueOf(pageInfo.getTotal()).intValue();
        int pages = pageInfo.getPages();
        int pageNum = pageInfo.getPageNum();
        List<WxFootprintListVO> list = pageInfo.getList();
        commonData.setTotal(total);
        commonData.setPages(pages);
        commonData.setLimit(limit);
        commonData.setPage(pageNum);
        commonData.setList(list);
        return BaseRespVo.ok(commonData);
    }

    @RequestMapping("delete")
    public BaseRespVo wxFootprintDelete(@RequestBody FootprintBO footprintBO){
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
//        Integer userId = 1;
        int affectedRows = wxFootprintService.deleteWxFootprint(footprintBO,userId);
        if (affectedRows == 1) {
            return BaseRespVo.ok(0, "成功");
        }
        return BaseRespVo.ok(205,"失败");
    }

}
